#!/bin/bash

# Slackware build script for xu4

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20230112 bkw: update for v1.2.1, after ~9 years there's a release!
# - no more disable_alt_x.diff, it works properly now.
# - always include the upgrade (u4upgrad.zip), since upstream does.
# - new dependencies boron and faun.
# - update man page and rename it u4 => xu4.
# - rework profile_path.diff.
# - update README and README-SBo.txt (remove obsolete info, add section
#   on running without pulse).
# - TODO: test with the GoG release (someone has to create a GoG account,
#   even though it's free-of-charge, and I'm not able to).

# 20211012 bkw: BUILD=3,
# - new-style icon.
# - binary in /usr/games.
# - man page in section 6.
# - disable Alt-x keystroke (it crashes the game).
# - "Press Alt-x to quit" => "Close window to quit".

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=xu4
VERSION=${VERSION:-1.2.1}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# LIBDIRSUFFIX isn't actually used for 1.2.1
if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# If we didn't supply this file, 'make install' would try to download it.
cat $CWD/u4upgrad.zip > u4upgrad.zip

# The xu4 source on the homepage (sourceforge) is missing a commit
# that's in the 1.2.1 tag on github, which makes it fail to compile.
# I could use the github source, but it doesn't include src/glv.
# Easier to just apply the github commit to the sf tarball.
patch -p1 < $CWD/really_1.2.1.diff

# Fix path to "profiles/" dir used by -p option. Patch makes it use
# ~/.config/xu4/profiles/ rather than ./profiles off the current dir.
patch -p1 < $CWD/profile_path.diff

# Use our flags, plus install to /usr/games (not /usr/bin).
sed -i -e "s,-O3,$SLKCFLAGS," src/Makefile
sed -i -e "/DESTDIR/s,/bin,/games,g" Makefile

# The configure script isn't autoconf (doesn't support the standard
# options) and the Makefile uses DESTDIR to mean PREFIX, more-or-less.
# We don't have to strip the binary (make install does it for us).
# Since we're not building from a git tree, we have to explicitly
# set VERSION (it only shows up in --help and the 'About' in the game).
./configure --prefix /usr
make DESTDIR=/usr VERSION=$VERSION
make install DESTDIR=$PKG/usr VERSION=$VERSION

# If we have the official release rather than the "dragon" one, repackage
# it with the same layout as the dragon release. Note that the official
# release isn't the default because there's no direct download link for
# the .info file (the site requires javascript + registration).
if [ -e $CWD/UltimaIV.zip ]; then
  sh $CWD/repackage.sh $CWD/UltimaIV.zip
fi

# The game data. This is the zip file, not the self-extracting exe (the
# exe lacks the documentation, for one thing).
cat $CWD/ultima4.zip > $PKG/usr/share/xu4/ultima4.zip

# Replace stock .desktop file with one that validates.
cat $CWD/xu4.desktop > $PKG/usr/share/applications/xu4.desktop

# New-style icons, made by downsizing icons/u4.png, except 128x128 was
# extracted from icons/xu4.icns in the source. I didn't downsize the
# Mac icon because it has a checkerboard background that looks good
# at 128x128, but horrid when downscaled.
# 48x48 was already installed, above.
HICOLOR=$PKG/usr/share/icons/hicolor/
mkdir -p $HICOLOR/64x64/apps
cat icons/u4.png > $HICOLOR/64x64/apps/$PRGNAM.png

for i in $CWD/icons/*.png; do
  px=$( basename $i | cut -d. -f1 )
  size=${px}x${px}
  dir=$HICOLOR/$size/apps
  mkdir -p $dir
  cat $i > $dir/u4.png
done

mkdir -p $PKG/usr/share/pixmaps
ln -s ../icons/hicolor/48x48/apps/xu4.png $PKG/usr/share/pixmaps/xu4.png

# Docs.
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC/developers
cp -a AUTHORS COPYING ChangeLog README* $PKGDOC
cp -a doc/* $PKGDOC/developers
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

# Include the upgrade's docs.
unzip -p $CWD/u4upgrad.zip Readme.txt > $PKGDOC/Readme-Upgrade.txt

# Include the PDF docs, if we have them.
if [ -e $CWD/ultima4_scanned_docs.zip ]; then
  mkdir -p $PKGDOC/pdf
  cd $PKGDOC/pdf
  unzip $CWD/ultima4_scanned_docs.zip '*.pdf'
  mv ultima4_scanned_docs/Ultima_IV_-_Cluebook_-_PC.pdf Hints.pdf
  mv ultima4_scanned_docs/Ultima_IV_-_Manual_-_PC.pdf History.pdf
  mv ultima4_scanned_docs/Ultima_IV_-_Spellbook_-_PC.pdf Wisdom.pdf
  rm -rf ultima4_scanned_docs
  cd -
fi

# Include the original Ultima IV docs.
cd $PKGDOC
unzip $CWD/ultima4.zip '*.txt' '*.doc' '*.bmp' '*.jpg' || true

# These docs only apply to running the original game on DOS or Windows. Bye.
rm -f Readme.txt Readme.doc

# The Word versions of the docs don't really offer much improvement
# over the plain text ones, but here they are.
mkdir ms_word_docs
mv *.doc ms_word_docs

# Fix permissions and line endings.
[ -e Map.bmp ] && chmod 644 Map.bmp
[ -e Map.jpg ] && chmod 644 Map.jpg
chmod 644 *.txt ms_word_docs/*.doc
sed -i 's/\r//' *.txt
cd -

# I also hate binaries that take options but have no man pages, so here's
# the one I made. Enjoy. I wish I'd used rst or pod for it...
mkdir -p $PKG/usr/man/man6
sed "s/@VERSION@/$VERSION/" $CWD/xu4.6 | gzip -9c - > $PKG/usr/man/man6/xu4.6.gz

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
